classdef udpInterface
    properties
        pluginName
        pluginNameCN
        appName
        udpSocket
    end
    
    methods
        function obj = udpInterface(plugin_name,plugin_cn_name,app_name)
            obj.pluginName = plugin_name;
            obj.pluginNameCN = plugin_cn_name;
            obj.appName = app_name;
            
            ip = '127.0.0.1';
            port = 9215;
            configPath = '../ipConfig.json';
            
            if exist(configPath,'file') == 2
                filedata = fileread(configPath);
                ipConfig = jsondecode(filedata);
                pluginNetWork = ipConfig.pluginNetWork;
                ip = pluginNetWork.ip;
                port = pluginNetWork.port;
            end
            
            obj.udpSocket = udp(ip, port);
            
             % open udp socket
            fopen(obj.udpSocket);
        end
        
        function delete(obj)
            % close udp socket
            fclose(obj.udpSocket);
        end
        
        function sendData(obj,jsonStr)
            disp(jsonStr);
            u8Str = unicode2native(jsonStr,'UTF-8');
            fwrite(obj.udpSocket, u8Str, 'char');
        end
        
        function [] = cbegin(obj)
            % create json object
            jsonObj = struct('pluginName', obj.pluginName,'pluginNameCN', obj.pluginNameCN, 'appName', obj.appName, 'command', 'begin', 'data', "");
            jsonStr = jsonencode(jsonObj);

            % send network data
            obj.sendData(jsonStr);
        end

        function [] = cend(obj)
            % create json object
            jsonObj = struct('pluginName', obj.pluginName,'pluginNameCN', obj.pluginNameCN, 'appName', obj.appName, 'command', 'end', 'data', "");
            jsonStr = jsonencode(jsonObj);
            
            % send network data
            obj.sendData(jsonStr);
        end

        function [] = clog(obj,str)
            % create json object
            jsonObj = struct('pluginName', obj.pluginName,'pluginNameCN', obj.pluginNameCN, 'appName', obj.appName, 'command', 'log', 'data', str);
            jsonStr = jsonencode(jsonObj);
            
            % send network data
            obj.sendData(jsonStr);
        end
        
        function [] = cerror(obj,type,msg,file,name,line)
             % create json object
            errorObj = struct('type', type, 'msg', msg, 'file', file, 'name', name,'line', line);
            jsonObj = struct('pluginName', obj.pluginName,'pluginNameCN', obj.pluginNameCN, 'appName', obj.appName, 'command', 'error', 'data', errorObj);
            jsonStr = jsonencode(jsonObj);
            
            % send network data
            obj.sendData(jsonStr);
        end

        function [] = cprogress(obj,progress_value)
            % create json object
            jsonObj = struct('pluginName', obj.pluginName, 'pluginNameCN', obj.pluginNameCN,'appName', obj.appName, 'command', 'progress', 'data', progress_value);
            jsonStr = jsonencode(jsonObj);
            
            % send network data
            obj.sendData(jsonStr);
        end

        function [] = cshowpic(obj,url)
            % create json object
            jsonObj = struct('pluginName', obj.pluginName, 'pluginNameCN', obj.pluginNameCN,'appName', obj.appName, 'command', 'showpic', 'data', url);
            jsonStr = jsonencode(jsonObj);
            
            % send network data
            obj.sendData(jsonStr);
        end
    end
end

